package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.*;
import com.xjc.business.entity.po.schadmin.AllocaDormitoryPo;
import com.xjc.business.entity.po.schadmin.BuildingPo;
import com.xjc.business.entity.vo.buildadmin.BuildDetailsVo;
import com.xjc.business.entity.vo.buildadmin.CheckBedNotAddRoomVo;
import com.xjc.business.entity.vo.schadmin.BuildingDetailsVo;
import com.xjc.business.entity.vo.schadmin.BuildingVo;
import com.xjc.business.entity.vo.schadmin.RestBedDetails;
import com.xjc.business.entity.vo.stu.CurrentSureDormitoryVo;
import com.xjc.business.entity.vo.stu.DormitoryVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.BuildingsMapper;
import com.xjc.system.result.Result;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author root
 * @description 针对表【buildings(楼宇表)】的数据库操作Service实现
 * @createDate 2025-01-25 10:24:31
 */
@Service
public class BuildingsServiceImpl extends ServiceImpl<BuildingsMapper, Buildings>
        implements BuildingsService {

    @Autowired
    @Lazy
    SchoolBuildingRelationsService schoolBuildingRelationsService;

    @Autowired
    @Lazy
    BuildingAdminRelationsService buildingAdminRelationsService;

    @Autowired
    @Lazy
    BuildingAdminsService buildingAdminsService;

    @Autowired
    @Lazy
    BuildingsService buildingsService;

    @Autowired
    @Lazy
    DormitoriesService dormitoriesService;

    @Autowired
    @Lazy
    BedsService bedsService;

    @Autowired
    @Lazy
    DormitoryBedRelationsService dormitoryBedRelationsService;

    @Autowired
    @Lazy
    BuildingDormitoryRelationsService buildingDormitoryRelationsService;

    @Autowired
    @Lazy
    WxUserService wxUserService;

    @Autowired
    @Lazy
    AllStudentRegsiterRelationsService allStudentRegsiterRelationsService;

    @Autowired
    @Lazy
    StudentsService studentsService;

    @Autowired
    @Lazy
    AllStudentStayRelationsService allStudentStayRelationsService;

    @Autowired
    @Lazy
    CollegesService collegesService;

    @Autowired
    @Lazy
    MajorsService majorsService;

    @Autowired
    @Lazy
    ClazzsService clazzsService;

    @Autowired
    @Lazy
    SchoolCollegeRelationsService schoolCollegeRelationsService;

    @Autowired
    @Lazy
    CollageMajorRelationsService collageMajorRelationsService;

    @Autowired
    @Lazy
    MajorClazzRelationsService majorClazzRelationsService;

    @Autowired
    @Lazy
    SchoolsService schoolsService;


    /**
     * 获取楼宇列表
     *
     * @param schoolId 学校id
     * @return 楼宇列表数据
     */
    @Override
    public Result<List<BuildingVo>> getBuildingList(Integer schoolId) {
        LambdaQueryWrapper<SchoolBuildingRelations> schoolBuildingRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolBuildingRelationsLambdaQueryWrapper.eq(SchoolBuildingRelations::getSchId, schoolId);
        List<SchoolBuildingRelations> schoolBuildingRelations = schoolBuildingRelationsService.list(schoolBuildingRelationsLambdaQueryWrapper);
        List<Integer> collectBidList = schoolBuildingRelations.stream().map(SchoolBuildingRelations::getBid).collect(Collectors.toList());

        List<BuildingVo> buildingVoList = new ArrayList<>();

        if (collectBidList.isEmpty()) {
            return Result.okWithData(buildingVoList);
        }

        LambdaQueryWrapper<Buildings> buildingsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingsLambdaQueryWrapper.in(Buildings::getId, collectBidList);
        buildingVoList = list(buildingsLambdaQueryWrapper).stream().map(buildings -> {
            BuildingVo buildingVo = new BuildingVo();
            buildingVo.setId(buildings.getId());
            buildingVo.setBno(buildings.getBno());
            buildingVo.setBfn(buildings.getBfn());
            buildingVo.setDn(buildings.getDn());
            buildingVo.setType(buildings.getType());
            buildingVo.setHasLiving(buildings.getHasLiving());
            buildingVo.setNotLiving(buildings.getNotLiving());
            buildingVo.setCreateTime(buildings.getCreateTime());

            LambdaQueryWrapper<BuildingAdminRelations> buildingAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            buildingAdminRelationsLambdaQueryWrapper.eq(BuildingAdminRelations::getBuildId, buildings.getId());
            BuildingAdminRelations buildingAdminRelationsFromDB = buildingAdminRelationsService.getOne(buildingAdminRelationsLambdaQueryWrapper);
            if (buildingAdminRelationsFromDB != null) {
                buildingVo.setAdminName(buildingAdminsService.getById(buildingAdminRelationsFromDB.getBuildAdminId()).getNickname());
            } else {
                buildingVo.setAdminName("未分配");
            }
            return buildingVo;
        }).collect(Collectors.toList());

        return Result.okWithData(buildingVoList);
    }

    /**
     * 新增楼宇
     *
     * @param buildingPo 楼宇信息
     * @return 反馈
     */
    @Transactional
    @Override
    public Result<String> addBuilding(BuildingPo buildingPo) {
        // 楼宇号
        Integer buildingPoBno = buildingPo.getBno();
        // 楼层数
        Integer buildingFloorNumber = buildingPo.getBfn();
        // 宿舍数
        Integer dormitoryNumber = buildingPo.getDn();
        // 每层宿舍数
        Integer dormitoryNumberPerFloor = dormitoryNumber / buildingFloorNumber;

        // 多出来的宿舍数
        Integer extraDormitoryNumber = dormitoryNumber % buildingFloorNumber;

        // 楼宇基本信息
        Buildings buildings = new Buildings();
        buildings.setBno(buildingPoBno);
        buildings.setBfn(extraDormitoryNumber > 0 ? buildingFloorNumber + 1 : buildingFloorNumber);
        buildings.setDn(dormitoryNumber);
        buildings.setType(buildingPo.getType());
        buildings.setHasLiving(0);
        buildings.setNotLiving(buildingPo.getDn() * 8);
        buildings.setCreateTime(new Date());
        buildings.setUpdateTime(new Date());
        save(buildings);

        // 多出来宿舍，在多加一层
        // TODO:给管理员两种选择，多加一层/放在最后一层
        if (extraDormitoryNumber > 0) {
            // 宿舍信息 i为楼层 j为单独宿舍号 k为床铺号 宿舍号组成：楼宇号+楼层+单独宿舍号(多位),例如 111 112 113 ... 1114
            for (int i = 1; i <= buildingFloorNumber; i++) {
                generateDormitory(buildingPoBno, dormitoryNumberPerFloor, buildings, i);
            }
            generateDormitory(buildingPoBno, extraDormitoryNumber, buildings, buildingFloorNumber + 1);
        } else {
            // 宿舍信息
            for (int i = 1; i <= buildingFloorNumber; i++) {
                generateDormitory(buildingPoBno, dormitoryNumberPerFloor, buildings, i);
            }
        }

        // 学校和楼宇关系信息
        SchoolBuildingRelations schoolBuildingRelations = new SchoolBuildingRelations();
        schoolBuildingRelations.setSchId(buildingPo.getSchoolId());
        schoolBuildingRelations.setBid(buildings.getId());
        schoolBuildingRelationsService.save(schoolBuildingRelations);

        return Result.okWithData("新增成功");
    }

    /**
     * 分配管理员
     *
     * @param adminId    管理员id
     * @param buildingId 楼宇id
     * @return 反馈
     */
    @Override
    public Result<String> associateAdmin(Integer adminId, Integer buildingId) {
        BuildingAdminRelations buildingAdminRelations = new BuildingAdminRelations();
        buildingAdminRelations.setBuildId(buildingId);
        buildingAdminRelations.setBuildAdminId(adminId);
        buildingAdminRelationsService.save(buildingAdminRelations);
        return Result.okWithData("分配成功");
    }

        /**
     * 获取楼宇详情
     * @param buildingId 楼宇id
     * @return 详情
     */
    @Override
    public Result<List<BuildingDetailsVo>> getBuildingDetails(Integer buildingId) {
        Buildings buildings = getById(buildingId);

        Integer buildFloorNumber = buildings.getBfn();

        LambdaQueryWrapper<BuildingDormitoryRelations> buildingDormitoryRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingDormitoryRelationsLambdaQueryWrapper.eq(BuildingDormitoryRelations::getBid, buildingId);

        // 获取该楼宇下的所有宿舍id
        List<Integer> dormitoryIdList = buildingDormitoryRelationsService.list(buildingDormitoryRelationsLambdaQueryWrapper).stream().map(BuildingDormitoryRelations::getDid).collect(Collectors.toList());

        List<BuildingDetailsVo> buildingDetailsVoList = new ArrayList<>();

        for (Integer i = 1; i <= buildFloorNumber; i++) {

            BuildingDetailsVo buildingDetailsVo = new BuildingDetailsVo();
            LambdaQueryWrapper<Dormitories> dormitoriesLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dormitoriesLambdaQueryWrapper.eq(Dormitories::getDfn, i);
            dormitoriesLambdaQueryWrapper.in(Dormitories::getId, dormitoryIdList);
            List<Dormitories> dormitoriesList = dormitoriesService.list(dormitoriesLambdaQueryWrapper);

            List<BuildingDetailsVo.DormitoryVo> dormitoryVoList = dormitoriesList.stream().map(dormitories -> {
                BuildingDetailsVo.DormitoryVo dormitoryVo = new BuildingDetailsVo.DormitoryVo();
                dormitoryVo.setId(dormitories.getId());
                dormitoryVo.setDno(dormitories.getDno());
                dormitoryVo.setStatus(dormitories.getStatus());

                LambdaQueryWrapper<DormitoryBedRelations> dormitoryBedRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                dormitoryBedRelationsLambdaQueryWrapper.eq(DormitoryBedRelations::getDid, dormitories.getId());
                List<Integer> bedIdList = dormitoryBedRelationsService.list(dormitoryBedRelationsLambdaQueryWrapper).stream().map(DormitoryBedRelations::getBedid).collect(Collectors.toList());

                List<BuildingDetailsVo.BedsVo> bedsVoList = bedsService.list().stream().filter(beds -> bedIdList.contains(beds.getId())).map(beds -> {
                    BuildingDetailsVo.BedsVo bedsVo = new BuildingDetailsVo.BedsVo();
                    bedsVo.setId(beds.getId());
                    bedsVo.setBno(beds.getBno());
                    bedsVo.setStatus(beds.getStatus());

                    LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getBedId, beds.getId());
                    AllStudentStayRelations allStudentStayRelations = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper);
                    if (allStudentStayRelations!=null){
                        Integer stuId = allStudentStayRelations.getStuId();

                        String name = studentsService.getById(stuId).getName();

                        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId, stuId);
                        AllStudentRegsiterRelations allStudentRegsiterRelations = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper);
                        Integer colId = allStudentRegsiterRelations.getColId();
                        Integer majId = allStudentRegsiterRelations.getMajId();
                        Integer clazzId = allStudentRegsiterRelations.getClazzId();

                        String colName = collegesService.getById(colId).getName();
                        String majName = majorsService.getById(majId).getName();
                        String clazzName = clazzsService.getById(clazzId).getNum() + "班";

                        bedsVo.setStuName(colName+"学院 "+majName+"专业 "+clazzName+" "+name);
                    }

                    return bedsVo;
                }).collect(Collectors.toList());
                dormitoryVo.setBedsList(bedsVoList);
                return dormitoryVo;
            }).collect(Collectors.toList());

            buildingDetailsVo.setBuildingFloor(i);
            buildingDetailsVo.setDormitoryList(dormitoryVoList);

            buildingDetailsVoList.add(buildingDetailsVo);
        }


        return Result.okWithData(buildingDetailsVoList);
    }

    /**
     * 获取宿舍列表
     *
     * @param id 学生微信id
     * @return 列表数据
     */
    @Override
    public Result<List<com.xjc.business.entity.vo.stu.BuildingVo>> getBuildingListWithStuId(Integer id) {

        Integer stuId = wxUserService.getById(id).getStuId();
        Integer sex = studentsService.getById(stuId).getSex();

        // 批量查询所有相关关系
        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId, stuId);
        List<AllStudentRegsiterRelations> allStudentRegsiterRelationsList = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper);
        if (allStudentRegsiterRelationsList.isEmpty()) {
            return Result.okWithData(new ArrayList<>());
        }

        Set<Integer> schIdSet = allStudentRegsiterRelationsList.stream().map(AllStudentRegsiterRelations::getSchId).collect(Collectors.toSet());

        LambdaQueryWrapper<SchoolBuildingRelations> schoolBuildingRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolBuildingRelationsLambdaQueryWrapper.in(SchoolBuildingRelations::getSchId, schIdSet);
        List<SchoolBuildingRelations> schoolBuildingRelationsList = schoolBuildingRelationsService.list(schoolBuildingRelationsLambdaQueryWrapper);
        if (schoolBuildingRelationsList.isEmpty()) {
            return Result.okWithData(new ArrayList<>());
        }

        Set<Integer> buildingIdSet = schoolBuildingRelationsList.stream().map(SchoolBuildingRelations::getBid).collect(Collectors.toSet());

        LambdaQueryWrapper<Buildings> buildingsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingsLambdaQueryWrapper.in(Buildings::getId, buildingIdSet);
        buildingsLambdaQueryWrapper.eq(Buildings::getType, sex);
        List<Buildings> buildingsList = list(buildingsLambdaQueryWrapper);
        if (buildingsList.isEmpty()) {
            return Result.okWithData(new ArrayList<>());
        }

        // 构建buildingId到building的映射
        Map<Integer, Buildings> buildingMap = buildingsList.stream().collect(Collectors.toMap(Buildings::getId, Function.identity()));

        // 批量查询宿舍关系
        LambdaQueryWrapper<BuildingDormitoryRelations> buildingDormitoryRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingDormitoryRelationsLambdaQueryWrapper.in(BuildingDormitoryRelations::getBid, buildingIdSet);
        List<BuildingDormitoryRelations> buildingDormitoryRelationsList = buildingDormitoryRelationsService.list(buildingDormitoryRelationsLambdaQueryWrapper);

        // 构建buildingId到dormitoryIdList的映射
        Map<Integer, List<Integer>> buildingToDormitoryMap = buildingDormitoryRelationsList.stream()
                .collect(Collectors.groupingBy(BuildingDormitoryRelations::getBid, Collectors.mapping(BuildingDormitoryRelations::getDid, Collectors.toList())));

        // 批量查询宿舍信息
        Set<Integer> dormitoryIdSet = buildingDormitoryRelationsList.stream().map(BuildingDormitoryRelations::getDid).collect(Collectors.toSet());
        LambdaQueryWrapper<Dormitories> dormitoriesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dormitoriesLambdaQueryWrapper.in(Dormitories::getId, dormitoryIdSet);
        List<Dormitories> dormitoriesList = dormitoriesService.list(dormitoriesLambdaQueryWrapper);

        // 构建dormitoryId到dormitory的映射
        Map<Integer, Dormitories> dormitoryMap = dormitoriesList.stream().collect(Collectors.toMap(Dormitories::getId, Function.identity()));

        // 批量查询床位关系
        LambdaQueryWrapper<DormitoryBedRelations> dormitoryBedRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dormitoryBedRelationsLambdaQueryWrapper.in(DormitoryBedRelations::getDid, dormitoryIdSet);
        List<DormitoryBedRelations> dormitoryBedRelationsList = dormitoryBedRelationsService.list(dormitoryBedRelationsLambdaQueryWrapper);

        // 构建dormitoryId到bedIdList的映射
        Map<Integer, List<Integer>> dormitoryToBedMap = dormitoryBedRelationsList.stream()
                .collect(Collectors.groupingBy(DormitoryBedRelations::getDid, Collectors.mapping(DormitoryBedRelations::getBedid, Collectors.toList())));

        // 批量查询床位信息
        Set<Integer> bedIdSet = dormitoryBedRelationsList.stream().map(DormitoryBedRelations::getBedid).collect(Collectors.toSet());
        List<Beds> bedsList = bedsService.listByIds(new ArrayList<>(bedIdSet));

        // 构建bedId到bed的映射
        Map<Integer, Beds> bedMap = bedsList.stream().collect(Collectors.toMap(Beds::getId, Function.identity()));

        List<com.xjc.business.entity.vo.stu.BuildingVo> buildingVoList = buildingsList.stream().map(buildings -> {
            com.xjc.business.entity.vo.stu.BuildingVo buildingVo = new com.xjc.business.entity.vo.stu.BuildingVo();
            buildingVo.setId(buildings.getId());
            buildingVo.setBno(buildings.getBno() + "号楼");

            Integer buildFloorNumber = buildings.getBfn();
            List<BuildingDetailsVo> buildingDetailsVoList = new ArrayList<>();

            for (Integer i = 1; i <= buildFloorNumber; i++) {
                BuildingDetailsVo buildingDetailsVo = new BuildingDetailsVo();
                List<Integer> dormitoryIdList = buildingToDormitoryMap.getOrDefault(buildings.getId(), Collections.emptyList());
                Integer finalI = i;
                List<Dormitories> dormitoriesInFloor = dormitoriesList.stream()
                        .filter(dormitories -> dormitories.getDfn().equals(finalI) && dormitoryIdList.contains(dormitories.getId()))
                        .collect(Collectors.toList());

                List<BuildingDetailsVo.DormitoryVo> dormitoryVoList = dormitoriesInFloor.stream().map(dormitories -> {
                    BuildingDetailsVo.DormitoryVo dormitoryVo = new BuildingDetailsVo.DormitoryVo();
                    dormitoryVo.setId(dormitories.getId());
                    dormitoryVo.setDno(dormitories.getDno());
                    dormitoryVo.setStatus(dormitories.getStatus());

                    List<Integer> bedIdList = dormitoryToBedMap.getOrDefault(dormitories.getId(), Collections.emptyList());
                    List<BuildingDetailsVo.BedsVo> bedsVoList = bedIdList.stream().map(bedId -> {
                        Beds bed = bedMap.get(bedId);
                        BuildingDetailsVo.BedsVo bedsVo = new BuildingDetailsVo.BedsVo();
                        bedsVo.setId(bed.getId());
                        bedsVo.setBno(bed.getBno());
                        bedsVo.setStatus(bed.getStatus());
                        return bedsVo;
                    }).collect(Collectors.toList());

                    dormitoryVo.setFeeNum((int) bedsVoList.stream().filter(bedsVo -> bedsVo.getStatus() == 1).count());
                    dormitoryVo.setBedsList(bedsVoList);
                    return dormitoryVo;
                }).collect(Collectors.toList());

                buildingDetailsVo.setBuildingFloor(i);
                buildingDetailsVo.setDormitoryList(dormitoryVoList);
                buildingDetailsVoList.add(buildingDetailsVo);
            }

            buildingVo.setBuildingDetailsList(buildingDetailsVoList);
            return buildingVo;
        }).collect(Collectors.toList());

        return Result.okWithData(buildingVoList);


//        Integer stuId = wxUserService.getById(id).getStuId();
//
//        Integer sex = studentsService.getById(stuId).getSex();
//
//        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId,stuId);
//        Integer schId = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper).getSchId();
//
//        LambdaQueryWrapper<SchoolBuildingRelations> schoolBuildingRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        schoolBuildingRelationsLambdaQueryWrapper.eq(SchoolBuildingRelations::getSchId,schId);
//        List<Integer> buildingIdList = schoolBuildingRelationsService.list(schoolBuildingRelationsLambdaQueryWrapper).stream().map(SchoolBuildingRelations::getBid).collect(Collectors.toList());
//        if(buildingIdList.isEmpty()){
//            return Result.okWithData(new ArrayList<>());
//        }
//
//        LambdaQueryWrapper<Buildings> buildingsLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        buildingsLambdaQueryWrapper.in(Buildings::getId,buildingIdList);
//        buildingsLambdaQueryWrapper.eq(Buildings::getType,sex);
//        List<Buildings> buildingsList = list(buildingsLambdaQueryWrapper);
//        if(buildingsList.isEmpty()){
//            return Result.okWithData(new ArrayList<>());
//        }
//
//        List<com.xjc.business.entity.vo.stu.BuildingVo> buildingVoList = buildingsList.stream().map(buildings -> {
//
//            com.xjc.business.entity.vo.stu.BuildingVo buildingVo = new com.xjc.business.entity.vo.stu.BuildingVo();
//
//            buildingVo.setId(buildings.getId());
//            buildingVo.setBno(buildings.getBno()+"号楼");
//
//            Integer buildFloorNumber = buildings.getBfn();
//
//            LambdaQueryWrapper<BuildingDormitoryRelations> buildingDormitoryRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            buildingDormitoryRelationsLambdaQueryWrapper.eq(BuildingDormitoryRelations::getBid, buildings.getId());
//
//            // 获取该楼宇下的所有宿舍id
//            List<Integer> dormitoryIdList = buildingDormitoryRelationsService.list(buildingDormitoryRelationsLambdaQueryWrapper).stream().map(BuildingDormitoryRelations::getDid).collect(Collectors.toList());
//
//            List<BuildingDetailsVo> buildingDetailsVoList = new ArrayList<>();
//
//            for (Integer i = 1; i <= buildFloorNumber; i++) {
//
//                BuildingDetailsVo buildingDetailsVo = new BuildingDetailsVo();
//                LambdaQueryWrapper<Dormitories> dormitoriesLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                dormitoriesLambdaQueryWrapper.eq(Dormitories::getDfn, i);
//                dormitoriesLambdaQueryWrapper.in(Dormitories::getId, dormitoryIdList);
//                List<Dormitories> dormitoriesList = dormitoriesService.list(dormitoriesLambdaQueryWrapper);
//
//                List<BuildingDetailsVo.DormitoryVo> dormitoryVoList = dormitoriesList.stream().map(dormitories -> {
//                    BuildingDetailsVo.DormitoryVo dormitoryVo = new BuildingDetailsVo.DormitoryVo();
//                    dormitoryVo.setId(dormitories.getId());
//                    dormitoryVo.setDno(dormitories.getDno());
//                    dormitoryVo.setStatus(dormitories.getStatus());
//
//                    AtomicReference<Integer> feeNum = new AtomicReference<>(0);
//
//                    LambdaQueryWrapper<DormitoryBedRelations> dormitoryBedRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                    dormitoryBedRelationsLambdaQueryWrapper.eq(DormitoryBedRelations::getDid, dormitories.getId());
//                    List<Integer> bedIdList = dormitoryBedRelationsService.list(dormitoryBedRelationsLambdaQueryWrapper).stream().map(DormitoryBedRelations::getBedid).collect(Collectors.toList());
//
//                    List<BuildingDetailsVo.BedsVo> bedsVoList = bedsService.list().stream().filter(beds -> bedIdList.contains(beds.getId())).map(beds -> {
//                        BuildingDetailsVo.BedsVo bedsVo = new BuildingDetailsVo.BedsVo();
//                        bedsVo.setId(beds.getId());
//                        bedsVo.setBno(beds.getBno());
//                        bedsVo.setStatus(beds.getStatus());
//                        if(beds.getStatus() == 1){
//                            feeNum.getAndSet(feeNum.get() + 1);
//                        }
//                        return bedsVo;
//                    }).collect(Collectors.toList());
//                    dormitoryVo.setFeeNum(feeNum.get());
//                    dormitoryVo.setBedsList(bedsVoList);
//                    return dormitoryVo;
//                }).collect(Collectors.toList());
//
//                buildingDetailsVo.setBuildingFloor(i);
//                buildingDetailsVo.setDormitoryList(dormitoryVoList);
//
//                buildingDetailsVoList.add(buildingDetailsVo);
//            }
//
//            buildingVo.setBuildingDetailsList(buildingDetailsVoList);
//
//            return buildingVo;
//        }).collect(Collectors.toList());
//
//
//        return Result.okWithData(buildingVoList);
    }

    /**
     * 确认选宿
     *
     * @param id    学生微信id
     * @param bedId 床位id
     * @return 反馈
     */
    @Override
    @Transactional
    public Result<String> sureSelectDormitory(Integer id, Integer bedId) {

        Integer stuId = wxUserService.getById(id).getStuId();

        LambdaQueryWrapper<DormitoryBedRelations> dormitoryBedRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dormitoryBedRelationsLambdaQueryWrapper.eq(DormitoryBedRelations::getBedid, bedId);
        DormitoryBedRelations dormitoryBedRelations = dormitoryBedRelationsService.getOne(dormitoryBedRelationsLambdaQueryWrapper);
        Integer dormitoryId = dormitoryBedRelations.getDid();

        LambdaQueryWrapper<BuildingDormitoryRelations> buildingDormitoryRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingDormitoryRelationsLambdaQueryWrapper.eq(BuildingDormitoryRelations::getDid, dormitoryId);
        BuildingDormitoryRelations buildingDormitoryRelations = buildingDormitoryRelationsService.getOne(buildingDormitoryRelationsLambdaQueryWrapper);
        Integer buildingId = buildingDormitoryRelations.getBid();

        AllStudentStayRelations allStudentStayRelations = new AllStudentStayRelations();
        allStudentStayRelations.setStuId(stuId);
        allStudentStayRelations.setBuildId(buildingId);
        allStudentStayRelations.setDormId(dormitoryId);
        allStudentStayRelations.setBedId(bedId);
        allStudentStayRelations.setStatus(1);
        allStudentStayRelations.setCreateTime(new Date());
        allStudentStayRelations.setUpdateTime(new Date());
        allStudentStayRelationsService.save(allStudentStayRelations);

        Beds beds = bedsService.getById(bedId);
        beds.setStatus(0);
        bedsService.updateById(beds);

        Buildings buildings = getById(buildingId);
        buildings.setHasLiving(buildings.getHasLiving() + 1);
        buildings.setNotLiving(buildings.getNotLiving() - 1);
        updateById(buildings);

        return Result.okWithData("选铺完成");
    }

    /**
     * 获取宿舍信息
     *
     * @param id 学生微信id
     * @return 宿舍信息
     */
    @Override
    public Result<DormitoryVo> getDormitoryInfo(Integer id) {
        Integer stuId = wxUserService.getById(id).getStuId();
        String name = studentsService.getById(stuId).getName();

        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId, stuId);
        AllStudentStayRelations allStudentStayRelations = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper);
        Integer buildId = allStudentStayRelations.getBuildId();
        Integer dormId = allStudentStayRelations.getDormId();
        Integer bedId = allStudentStayRelations.getBedId();
        Integer status = allStudentStayRelations.getStatus();

        Integer bno = getById(buildId).getBno();
        Integer dno = dormitoriesService.getById(dormId).getDno();
        Integer bno1 = bedsService.getById(bedId).getBno();

        DormitoryVo dormitoryVo = new DormitoryVo();
        dormitoryVo.setStuName(name);
        dormitoryVo.setLh(bno + "号楼");
        dormitoryVo.setSsh(dno + "");
        dormitoryVo.setCwh(bno1 + "号");
        dormitoryVo.setStatus(status == 0 ? "换铺中" : "正常");

        return Result.okWithData(dormitoryVo);
    }

    /**
     * 获取空余床位详情
     * @param schoolId 学校id
     * @return 详情
     */
    @Override
    public Result<RestBedDetails> getRestBedDetails(Integer schoolId) {
        RestBedDetails restBedDetails = new RestBedDetails();

        List<AllStudentStayRelations> allStudentStayRelationsList = allStudentStayRelationsService.list();
        List<Integer> stayStuIdList = allStudentStayRelationsList.stream().map(AllStudentStayRelations::getStuId).collect(Collectors.toList());

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getSchId, schoolId);
        allStudentRegsiterRelationsLambdaQueryWrapper.in(!stayStuIdList.isEmpty(),AllStudentRegsiterRelations::getStuId, stayStuIdList);
        List<Integer> integerList = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper).stream().map(AllStudentRegsiterRelations::getStuId).collect(Collectors.toList());

        LambdaQueryWrapper<Students> studentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(!stayStuIdList.isEmpty()){

            studentsLambdaQueryWrapper.notIn(!integerList.isEmpty(),Students::getId, integerList);
        }

        Map<Integer, List<Students>> collect = studentsService.list(studentsLambdaQueryWrapper).stream().collect(Collectors.groupingBy(Students::getSex));
        List<Students> mans = collect.get(1);
        List<Students> womans = collect.get(0);
        restBedDetails.setTotalManNum(mans==null?0:mans.size());
        restBedDetails.setTotalWomanNum(womans==null?0:womans.size());
        restBedDetails.setTotalRestNum(restBedDetails.getTotalManNum() + restBedDetails.getTotalWomanNum());

        List<Integer> stayBedIdList = allStudentStayRelationsList.stream().map(AllStudentStayRelations::getBedId).collect(Collectors.toList());

        LambdaQueryWrapper<Buildings> buildingsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingsLambdaQueryWrapper.eq(Buildings::getType,1);
        // 男生宿舍楼
        List<Integer> manBuildIdList = list(buildingsLambdaQueryWrapper).stream().map(Buildings::getId).collect(Collectors.toList());

        LambdaQueryWrapper<Buildings> buildingsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        buildingsLambdaQueryWrapper2.eq(Buildings::getType,0);
        // 女生宿舍楼
        List<Integer> wonmanBuildIdList = list(buildingsLambdaQueryWrapper2).stream().map(Buildings::getId).collect(Collectors.toList());

        long manBuildCount = getSexBuildCount(schoolId, stayBedIdList, manBuildIdList);
        long wonmanBuildCount = getSexBuildCount(schoolId, stayBedIdList, wonmanBuildIdList);
        restBedDetails.setTotalWoManRestBedNum((int) wonmanBuildCount);
        restBedDetails.setTotalManRestBedNum((int) manBuildCount);
        restBedDetails.setTotalRestBedNum(restBedDetails.getTotalManRestBedNum() + restBedDetails.getTotalWoManRestBedNum());

        return Result.okWithData(restBedDetails);
    }

    /**
     * 一键分配宿舍
     * @param allocaDormitoryPo 宿舍分配信息
     * @return 反馈
     */
    @Override
    @Transactional
    public Result<String> allocationDormitoryWithOneClick(AllocaDormitoryPo allocaDormitoryPo) {

        Integer schoolId = allocaDormitoryPo.getSchoolId();

        Integer type = allocaDormitoryPo.getType();

        // 数量判断
        RestBedDetails restBedDetails = getRestBedDetails(schoolId).getData();
        Integer totalManNum = restBedDetails.getTotalManNum();
        Integer totalWomanNum = restBedDetails.getTotalWomanNum();
        Integer totalManRestBedNum = restBedDetails.getTotalManRestBedNum();
        Integer totalWoManRestBedNum = restBedDetails.getTotalWoManRestBedNum();

        if(type == 1){
            if(totalManNum > totalManRestBedNum){
                return Result.ok("男生宿舍剩余床位不足，请添加宿舍床位");
            }
        } else if (type == 0) {
            if(totalWomanNum > totalWoManRestBedNum){
                return Result.ok("女生宿舍剩余床位不足，请添加宿舍床位");
            }
        }else{
            if(totalManNum > totalManRestBedNum){
                return Result.ok("男生宿舍剩余床位不足，请添加宿舍床位");
            }
            if(totalWomanNum > totalWoManRestBedNum){
                return Result.ok("女生宿舍剩余床位不足，请添加宿舍床位");
            }
        }

        // 1、学生id列表-有序：院系<专业<班级<学生>>>
        List<Integer> studentIdList = new ArrayList<>();

        // 已分配学生id
        List<AllStudentStayRelations> allStudentStayRelationsList = allStudentStayRelationsService.list();
        List<Integer> stayStuIdList = allStudentStayRelationsList.stream().map(AllStudentStayRelations::getStuId).collect(Collectors.toList());

        // 男生id
        LambdaQueryWrapper<Students> studentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentsLambdaQueryWrapper.eq(Students::getSex, 1);
        List<Integer> manStuIdList = studentsService.list(studentsLambdaQueryWrapper).stream().map(Students::getId).collect(Collectors.toList());

        // 女生id
        LambdaQueryWrapper<Students> studentsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        studentsLambdaQueryWrapper2.eq(Students::getSex, 0);
        List<Integer> wonmanStuIdList = studentsService.list(studentsLambdaQueryWrapper2).stream().map(Students::getId).collect(Collectors.toList());

        LambdaQueryWrapper<SchoolCollegeRelations> schoolCollegeRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolCollegeRelationsLambdaQueryWrapper.eq(SchoolCollegeRelations::getSchId, schoolId);
        // 学院
        schoolCollegeRelationsService.list(schoolCollegeRelationsLambdaQueryWrapper).stream().forEach(schoolCollegeRelations -> {
            Integer colId = schoolCollegeRelations.getColId();
            LambdaQueryWrapper<CollageMajorRelations> collageMajorRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            collageMajorRelationsLambdaQueryWrapper.eq(CollageMajorRelations::getColId, colId);
            // 专业
            collageMajorRelationsService.list(collageMajorRelationsLambdaQueryWrapper).stream().forEach(collageMajorRelations -> {
                Integer majId = collageMajorRelations.getMajId();
                LambdaQueryWrapper<MajorClazzRelations> majorClazzRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                majorClazzRelationsLambdaQueryWrapper.eq(MajorClazzRelations::getMajId, majId);
                // 班级
                majorClazzRelationsService.list(majorClazzRelationsLambdaQueryWrapper).stream().forEach(majorClazzRelations -> {
                    // 学生
                    Integer clId = majorClazzRelations.getClId();
                    LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getClazzId, clId);
                    allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper).stream().forEach(allStudentRegsiterRelations -> {
                        studentIdList.add(allStudentRegsiterRelations.getStuId());
                    });
                });
            });

        });

        // 移除已分配
        studentIdList.removeAll(stayStuIdList);

        // 性别要求-保留
        if (type == 1){
            //男生
            studentIdList.retainAll(manStuIdList);
        } else if (type == 0) {
            //女生
            studentIdList.retainAll(wonmanStuIdList);
        }

        if (studentIdList.isEmpty()){
            return Result.ok("没有符合要求的学生");
        }

        //2、分配宿舍
        if(type == 2){
            // 所有性别
            // 男
            List<Integer> tempManIdList = new ArrayList<>(studentIdList);
            tempManIdList.retainAll(manStuIdList);
            buildingsService.allocateBed(schoolId, allStudentStayRelationsList, tempManIdList,1);

            // 女
            List<Integer> tempWonManIdList = new ArrayList<>(studentIdList);
            tempWonManIdList.retainAll(wonmanStuIdList);
            buildingsService.allocateBed(schoolId, allStudentStayRelationsList, tempWonManIdList,0);

        }else {
            // 只分男生或女生
            buildingsService.allocateBed(schoolId, allStudentStayRelationsList, studentIdList,type);
        }

        return Result.okWithData("分配宿舍完成");
    }

    /**
     * 分配宿舍
     */
    @Override
    @Transactional
    public void allocateBed(Integer schoolId, List<AllStudentStayRelations> allStudentStayRelationsList, List<Integer> sexStuIdList,Integer type) {
        List<Integer> stayBedIdList = allStudentStayRelationsList.stream().map(AllStudentStayRelations::getBedId).collect(Collectors.toList());
        LambdaQueryWrapper<Buildings> buildingsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingsLambdaQueryWrapper.eq(Buildings::getType, type);
        List<Integer> manBuildIdList = list(buildingsLambdaQueryWrapper).stream().map(Buildings::getId).collect(Collectors.toList());

        LambdaQueryWrapper<SchoolBuildingRelations> schoolBuildingRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolBuildingRelationsLambdaQueryWrapper.eq(SchoolBuildingRelations::getSchId, schoolId);
        schoolBuildingRelationsLambdaQueryWrapper.in(!manBuildIdList.isEmpty(),SchoolBuildingRelations::getBid,manBuildIdList);

        // 该学校的楼宇集合
        List<Integer> buildIdList = schoolBuildingRelationsService.list(schoolBuildingRelationsLambdaQueryWrapper).stream().map(SchoolBuildingRelations::getId).collect(Collectors.toList());
        LambdaQueryWrapper<BuildingDormitoryRelations> buildingDormitoryRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingDormitoryRelationsLambdaQueryWrapper.in(BuildingDormitoryRelations::getBid, buildIdList);
        // 该学校的宿舍集合
        List<Integer> dormIdList = buildingDormitoryRelationsService.list(buildingDormitoryRelationsLambdaQueryWrapper).stream().map(BuildingDormitoryRelations::getDid).collect(Collectors.toList());
        LambdaQueryWrapper<DormitoryBedRelations> dormitoryBedRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dormitoryBedRelationsLambdaQueryWrapper.in(DormitoryBedRelations::getDid, dormIdList);
        // 去除已被选床位
        dormitoryBedRelationsLambdaQueryWrapper.notIn(!stayBedIdList.isEmpty(),DormitoryBedRelations::getBedid, stayBedIdList);
        Page<DormitoryBedRelations> dormitoryBedRelationsPage = dormitoryBedRelationsService.page(new Page<>(1, sexStuIdList.size()));
        dormitoryBedRelationsService.page(dormitoryBedRelationsPage, dormitoryBedRelationsLambdaQueryWrapper);

        List<Integer> bedIdList =  dormitoryBedRelationsPage.getRecords().stream().map(DormitoryBedRelations::getBedid).collect(Collectors.toList());

        List<AllStudentStayRelations> allStudenWitetStayRelationsList = new ArrayList<>();

        Map<Integer,Integer> buildNumUpdate = new HashMap<>();

        Map<Integer,Integer> dormitoryNumUpdate = new HashMap<>();

        for (int i = 0; i < sexStuIdList.size(); i++) {
            Integer bedId = bedIdList.get(i);
            AllStudentStayRelations allStudentStayRelations = new AllStudentStayRelations();
            allStudentStayRelations.setStuId(sexStuIdList.get(i));
            allStudentStayRelations.setBedId(bedId);

            LambdaQueryWrapper<DormitoryBedRelations> dormitoryBedRelationsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            dormitoryBedRelationsLambdaQueryWrapper2.eq(DormitoryBedRelations::getBedid, bedId);
            Integer did = dormitoryBedRelationsService.getOne(dormitoryBedRelationsLambdaQueryWrapper2).getDid();
            allStudentStayRelations.setDormId(did);

            dormitoryNumUpdate.put(did,dormitoryNumUpdate.getOrDefault(did,0)+1);

            LambdaQueryWrapper<BuildingDormitoryRelations> buildingDormitoryRelationsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            buildingDormitoryRelationsLambdaQueryWrapper2.eq(BuildingDormitoryRelations::getDid, did);
            Integer bid = buildingDormitoryRelationsService.getOne(buildingDormitoryRelationsLambdaQueryWrapper2).getBid();
            allStudentStayRelations.setBuildId(bid);

            buildNumUpdate.put(bid,buildNumUpdate.getOrDefault(bid,0)+1);

            allStudentStayRelations.setStatus(1);
            allStudentStayRelations.setCreateTime(new Date());
            allStudentStayRelations.setUpdateTime(new Date());

            allStudenWitetStayRelationsList.add(allStudentStayRelations);
        }

        // 更新学生住宿关系表
        allStudentStayRelationsService.saveBatch(allStudenWitetStayRelationsList);

        // 更新床位状态
        for (Integer bedId : bedIdList) {
            LambdaQueryWrapper<Beds> bedsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            bedsLambdaQueryWrapper.eq(Beds::getId, bedId);
            Beds beds = bedsService.getOne(bedsLambdaQueryWrapper);
            beds.setStatus(0);
            beds.setUpdateTime(new Date());
            bedsService.updateById(beds);
        }

        //更新宿舍状态
        for (Integer did : dormitoryNumUpdate.keySet()) {
            // todo 暂时为8人一宿舍，后续给管理员可选
            if(dormitoryNumUpdate.get(did) == 8){
                Dormitories dormitories = dormitoriesService.getById(did);
                dormitories.setStatus(0);
                dormitories.setUpdateTime(new Date());
                dormitoriesService.updateById(dormitories);
            }
        }

        // 更新楼宇人数表
        for (Integer bid : buildNumUpdate.keySet()) {
            LambdaQueryWrapper<Buildings> buildingsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            buildingsLambdaQueryWrapper2.eq(Buildings::getId, bid);
            Buildings buildings2 = getOne(buildingsLambdaQueryWrapper2);
            buildings2.setHasLiving(buildings2.getHasLiving() + buildNumUpdate.get(bid));
            buildings2.setNotLiving(buildings2.getNotLiving() - buildNumUpdate.get(bid));
            buildings2.setUpdateTime(new Date());
            updateById(buildings2);
        }
    }

    /**
     * 获取学校-宿舍楼名称
     * @param id 宿舍楼d
     * @return 学校-宿舍楼名称
     */
    @Override
    public Result<String> getBuildName(Integer id) {
        Buildings buildings = buildingsService.getById(id);
        Integer bno = buildings.getBno();
        Integer type = buildings.getType();

        LambdaQueryWrapper<SchoolBuildingRelations> schoolBuildingRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolBuildingRelationsLambdaQueryWrapper.eq(SchoolBuildingRelations::getBid, id);
        Integer schId = schoolBuildingRelationsService.getOne(schoolBuildingRelationsLambdaQueryWrapper).getSchId();

        String schoolName = schoolsService.getById(schId).getSchoolName();

        return Result.okWithData(schoolName+"-"+bno+"号"+(type == 0?"女生":"男生")+"宿舍楼");
    }

    /**
     * 去除“已被选宿舍床位列表”之外的宿舍列表
     * @param buildingId 楼宇id
     * @param hasAddRoomIdList 已被选宿舍床位列表
     * @return 列表数据
     */
    @Override
    public List<CheckBedNotAddRoomVo> getBuildDormitoryListWithCheckBed(Integer buildingId, List<Integer> hasAddRoomIdList) {

        List<CheckBedNotAddRoomVo> buildDetailsVoList = new ArrayList<>();

        LambdaQueryWrapper<BuildingDormitoryRelations> buildingDormitoryRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingDormitoryRelationsLambdaQueryWrapper.eq(BuildingDormitoryRelations::getBid, buildingId);
        List<Integer> dIdList = buildingDormitoryRelationsService.list(buildingDormitoryRelationsLambdaQueryWrapper).stream().map(BuildingDormitoryRelations::getDid).collect(Collectors.toList());

        Buildings buildings = getById(buildingId);
        Integer bfn = buildings.getBfn();
        for (Integer i = 1; i <= bfn; i++) {
            CheckBedNotAddRoomVo buildDetailsVo = new CheckBedNotAddRoomVo();
            buildDetailsVo.setValue(i+"楼");

            LambdaQueryWrapper<Dormitories> dormitoriesLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dormitoriesLambdaQueryWrapper.eq(Dormitories::getDfn, i);
            dormitoriesLambdaQueryWrapper.in(Dormitories::getId, dIdList);
            dormitoriesLambdaQueryWrapper.notIn(!hasAddRoomIdList.isEmpty(),Dormitories::getId,hasAddRoomIdList);
            dormitoriesLambdaQueryWrapper.orderByAsc(Dormitories::getDno);

            List<CheckBedNotAddRoomVo.DormitoryVo> dormitoryVoList = dormitoriesService.list(dormitoriesLambdaQueryWrapper).stream().map(dormitories -> {
                CheckBedNotAddRoomVo.DormitoryVo dormitoryVo = new CheckBedNotAddRoomVo.DormitoryVo();
                dormitoryVo.setValue(dormitories.getId());
                dormitoryVo.setLabel(dormitories.getDno()+"宿舍");

                return dormitoryVo;
            }).collect(Collectors.toList());

            buildDetailsVo.setChildren(dormitoryVoList);

            buildDetailsVoList.add(buildDetailsVo);
        }

        return buildDetailsVoList;
    }

    /**
     * 构造宿舍
     */
    private void generateDormitory(Integer buildingPoBno, Integer dormitoryNumberPerFloor, Buildings buildings, int i) {
        for (int j = 1; j <= dormitoryNumberPerFloor; j++) {
            // 宿舍信息
            Dormitories dormitories = new Dormitories();
            dormitories.setDno(Integer.valueOf((buildingPoBno + "" + i + "" + j)));
            dormitories.setStatus(1);
            dormitories.setDfn(i);
            dormitories.setCreateTime(new Date());
            dormitories.setUpdateTime(new Date());
            dormitoriesService.save(dormitories);
            // 默认8床位
            // TODO:管理员可以配置床位数
            for (int k = 1; k <= 8; k++) {
                // 床铺信息
                Beds beds = new Beds();
                beds.setBno(k);
                beds.setStatus(1);
                beds.setCreateTime(new Date());
                beds.setUpdateTime(new Date());
                bedsService.save(beds);
                // 宿舍和床铺关系信息
                DormitoryBedRelations dormitoryBedRelations = new DormitoryBedRelations();
                dormitoryBedRelations.setDid(dormitories.getId());
                dormitoryBedRelations.setBedid(beds.getId());
                dormitoryBedRelationsService.save(dormitoryBedRelations);
            }

            // 楼宇和宿舍关系信息
            BuildingDormitoryRelations buildingDormitoryRelations = new BuildingDormitoryRelations();
            buildingDormitoryRelations.setBid(buildings.getId());
            buildingDormitoryRelations.setDid(dormitories.getId());
            buildingDormitoryRelationsService.save(buildingDormitoryRelations);
        }

    }

    /**
     * 指定性别楼宇数量
     */
    private long getSexBuildCount(Integer schoolId, List<Integer> stayBedIdList, List<Integer> manBuildIdList) {
        if (manBuildIdList.size() == 0){
            return 0;
        }

        LambdaQueryWrapper<SchoolBuildingRelations> schoolBuildingRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolBuildingRelationsLambdaQueryWrapper.eq(SchoolBuildingRelations::getSchId, schoolId);
        schoolBuildingRelationsLambdaQueryWrapper.in(SchoolBuildingRelations::getBid, manBuildIdList);
        // 该学校的楼宇集合
        List<Integer> buildIdList = schoolBuildingRelationsService.list(schoolBuildingRelationsLambdaQueryWrapper).stream().map(SchoolBuildingRelations::getId).collect(Collectors.toList());
        LambdaQueryWrapper<BuildingDormitoryRelations> buildingDormitoryRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingDormitoryRelationsLambdaQueryWrapper.in(BuildingDormitoryRelations::getBid, buildIdList);
        // 该学校的宿舍集合
        List<Integer> dormIdList = buildingDormitoryRelationsService.list(buildingDormitoryRelationsLambdaQueryWrapper).stream().map(BuildingDormitoryRelations::getDid).collect(Collectors.toList());
        LambdaQueryWrapper<DormitoryBedRelations> dormitoryBedRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dormitoryBedRelationsLambdaQueryWrapper.in(DormitoryBedRelations::getDid, dormIdList);
        // 去除已被选床位
        dormitoryBedRelationsLambdaQueryWrapper.notIn(!stayBedIdList.isEmpty(),DormitoryBedRelations::getBedid, stayBedIdList);
        long count = dormitoryBedRelationsService.count(dormitoryBedRelationsLambdaQueryWrapper);
        return count;
    }
}